package com.baofu.paycore.biz.engine.processor.impl;

import com.baofu.clear.service.facade.model.RefundReqDTO;
import com.baofu.paycore.biz.engine.command.impl.RefundBankCommand;
import com.baofu.paycore.biz.engine.processor.BaseProcessor;
import com.baofu.paycore.biz.engine.result.CommandResult;
import com.baofu.paycore.biz.enums.CommandProcessorEnum;
import com.baofu.paycore.biz.external.clear.ClearRefundService;
import com.baofu.paycore.biz.external.clear.converter.ClearRequestConverter;
import com.baofu.paycore.biz.external.clear.model.ClearFundResBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 银行卡退款处理器
 * <p>
 * </p>
 * User: kwl Date: 2016/6/14 ProjectName: paycore Version: 5.0.0
 */
@Slf4j
@Service
public class BankRefundProcessor implements BaseProcessor<RefundBankCommand> {

    /**
     * 清分退款接口
     */
    @Autowired
    private ClearRefundService clearRefundService;

    /**
     * 获取处理器类型
     *
     * @return    处理器类型
     */
    @Override
    public CommandProcessorEnum getProcessorType() {
        return CommandProcessorEnum.BANK_REFUND_PROCESSOR;
    }

    /**
     * 银行卡退款,走清分退款或提现流程
     *
     * @param  refundBankCommand    退款支付命令
     * @return Map
     */
    @Override
    public Map<String, CommandResult> doProcess(RefundBankCommand refundBankCommand) {

        log.debug("银行卡退款处理器处理流程, parameter:{}",refundBankCommand);
        //1、封装清分退款请求参数
        RefundReqDTO refundReqDTO = ClearRequestConverter.clearRefundDTOConverter(refundBankCommand);

        //2、调用清分退款接口
        ClearFundResBO clearFundResBO = clearRefundService.clearRefund(refundReqDTO);

        //3、指令返回对象封装
        Map<String, CommandResult> result = new HashMap<>();
        CommandResult commandResult = new CommandResult();
        commandResult.setResponseNo(clearFundResBO.getResponseNo());
        commandResult.setResultCode(clearFundResBO.getErrorCode());
        commandResult.setResultMsg(clearFundResBO.getErrorMsg());
        commandResult.setResponseDate(clearFundResBO.getResponseDate());
        commandResult.setCommand(refundBankCommand);
        commandResult.setRealRtnFlag(clearFundResBO.isRealFlag());
        commandResult.setStatus(clearFundResBO.getPayStat());

        result.put(refundBankCommand.getCommandNo(),commandResult);
        return result;
    }

    /**
     * 执行取消方法
     *
     * @param commands    支付命令
     * @return map        指令执行结果
     */
    @Override
    public Map<String, CommandResult> doCancel(List<RefundBankCommand> commands) {
        return null;
    }
}
